package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.CommentReplyDO;
import io.github.talelin.latticy.service.CommentReplyService;
import io.github.talelin.latticy.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import io.github.talelin.latticy.model.CommentDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @author generator@lms
* @since 2021-04-23
*/
@RestController
@RequestMapping("/v1/comment")
@PermissionModule("评论")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private CommentReplyService commentReplyService;

    @PutMapping("/{id}")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @PermissionMeta("删除评论")
    @GroupRequired
    @Logger(template = "{user.nickname}删除了一条评论")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        CommentDO comment = commentService.getById(id);
        if (comment == null) {
            throw new NotFoundException(40000);
        }
        commentService.removeById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public CommentDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        CommentDO comment = commentService.getById(id);
        List<CommentReplyDO> commentReplyList = commentReplyService.getBaseMapper().selectList(new QueryWrapper<CommentReplyDO>()
                .eq("comment_id", id));
        comment.setReplyList(commentReplyList);
        return comment;
    }

    @GetMapping("/page")
    public PageResponseVO<CommentDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<CommentDO> pager = new Page<>(page, count);
        IPage<CommentDO> paging = commentService.getBaseMapper().selectPage(pager, new QueryWrapper<CommentDO>()
                .eq("type",1));
        return PageUtil.build(paging);
    }

    @GetMapping("/auditPage")
    @PermissionMeta("审核评论")
    @GroupRequired
    @Logger(template = "{user.nickname}审核了一条评论")
    public PageResponseVO<CommentDO> page1(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<CommentDO> pager = new Page<>(page, count);
        IPage<CommentDO> paging = commentService.getBaseMapper().selectPage(pager, new QueryWrapper<CommentDO>()
                .eq("type",0));
        return PageUtil.build(paging);
    }

}
